﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using SportsStore.WebUI.Binder;
using SportsStore.WebUI.Infrastructure;
using SportsStore.Domain.Entities;

namespace SportsStore.WebUI
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
           // routes.RouteExistingFiles = false;
            #region Product Page Routing
            routes.MapRoute(null, "", new {controller = "Home", action = "Index"});
            routes.MapRoute(null, "Home", new {controller = "Home", action = "Index"});
            routes.MapRoute(null, "Product", new {controller = "Product", action = "ProductsInformation"});
            routes.MapRoute(null, "Product/List/{category}", new {controller = "Product", action = "List",page=1});
            routes.MapRoute(null, "Product/List/{category}/Page{page}", new {controller = "Product", action = "List"},
                            new {page = @"\d+"});
            routes.MapRoute(null, "Product/Item/{productId}",
                            new {controller = "Product", action = "Item", productId = UrlParameter.Optional},
                            new {productId = @"\d+"});
            routes.MapRoute(null, "Product/GetImage/{productId}", new {controller = "Product", action = "GetImage", productId = UrlParameter.Optional}, new {productId = @"\d+"});
            #endregion

            #region Download Page Routing

            //routes.MapRoute(null, "Download", new {controller = "Download", action = "List", page = 1});
            routes.MapRoute(null, "Download/List/{category}", new {controller = "Download", action = "List", page = 1});
            routes.MapRoute(null, "Download/List/{category}/Page{page}", new {controller = "Download", action = "List"},
                            new {page = @"\d+"});
            #endregion

            routes.MapRoute(null, "Contact", new {Controller = "Contact", action = "ContactUs"});

            routes.MapRoute(null, "Company/BriefIntro", new {controller = "Company", action = "BriefIntro"});
            /*
            routes.MapRoute(null, "", new {Controller = "Product", action = "List", category = (string) null, page = 1});
            routes.MapRoute(null, "Item/{productId}",
                            new { Controller = "Product", action = "Item", productId = UrlParameter.Optional },
                            new { productId = @"\d+" });
            routes.MapRoute(null, "Page{page}", new {Controller = "Product", action = "List", category=(string)null}, new {page =@"\d+"});
            routes.MapRoute(null, "{category}", new {Controller = "Product", action = "List", page = 1});
            routes.MapRoute(null, "{category}/Page{page}", new {Controller = "Product", action = "List"},
                            new {page = @"\d+"});*/
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Product", action = "List", id = UrlParameter.Optional } // Parameter defaults
            );
            
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();


            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
            ModelBinders.Binders.Add(typeof(Cart),new CartModelBinder());
        }
    }
}